package com.sbtr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.business.equip.dto.EquipEntityDto;
import com.sbtr.business.equip.entity.EquipEntity;
import com.sbtr.business.equip.service.EquipEntityService;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

/**
 * 设备实例信息字段列表 前端控制器
 *
 * @author dgl
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/equipEntity")
@ApiIgnore
@Api(tags = "设备实例信息字段列表")
public class EquipEntityController {

    @Autowired
    private EquipEntityService equipEntityService;

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "设备实例信息字段列表列表")
    public Response<PageDto<EquipEntity>> pageList(EquipEntityDto equipEntity){
        return Response.success(equipEntityService.pageList(equipEntity));
    }

    @GetMapping("/getById")
    @ApiOperation(httpMethod = "GET", value = "获取设备实例信息字段列表信息")
    public Response<EquipEntity> getById(Long id){
        return Response.success(equipEntityService.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "设备实例信息字段列表保存")
    public Response<EquipEntity> save(@RequestBody EquipEntity equipEntity){
        QueryWrapper<EquipEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("equip_code",equipEntity.getEquipCode());
        int count = equipEntity.selectCount(wrapper);
        if(count>0){
            throw new BDException("已经存在该设备编号");
        }

        equipEntity.setId(null);
        if(equipEntity.insert()){
            return Response.success(equipEntity);
        }else throw new BDException("保存失败");
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "设备实例信息字段列表修改")
    public Response<String> update(@RequestBody EquipEntity equipEntity){
        if(!equipEntity.updateById()){
            throw new BDException("修改失败");
        }
        return Response.success("修改成功");
    }
    @PostMapping("/delete")
    @ApiOperation(httpMethod = "POST", value = "设备实例信息字段列表删除")
    public Response<String> delete(Long id){
        if(!equipEntityService.removeById(id)){
            throw new BDException("删除失败");
        }
        return Response.success("删除成功");
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(httpMethod = "POST", value = "设备实例信息字段列表批量删除,逗号分隔ps:'1,2,3'")
    public Response<String> deleteByIds(String ids){

        List<String> list = Arrays.asList(ids.split(","));

        if(!equipEntityService.removeByIds(list)){
            throw new BDException("删除失败");
        }
        return Response.success("删除成功");
    }
}

